package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.BaseCategory3;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.vo.album.AttributeVo;
import com.atguigu.tingshu.vo.album.BaseCategoryVo;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@Tag(name = "分类管理")
@RestController
@RequestMapping(value="/api/album/category")
@SuppressWarnings({"unchecked", "rawtypes"})
public class BaseCategoryApiController {
	
	@Autowired
	private BaseCategoryService baseCategoryService;

	@Schema(description = "查询分类树形结构")
	@GetMapping("/getBaseCategoryList")
	public Result getBaseCategoryList() {
		List<BaseCategoryVo> categoryList = baseCategoryService.getBaseCategoryList(null);
		return Result.ok(categoryList);
	}

	@Schema(description = "根据一级分类查询对应的属性")
	@GetMapping("/findAttribute/{categoryId}")
	public Result findAttribute(@PathVariable("categoryId") Long categoryId) {
		List<AttributeVo> attributeList = baseCategoryService.findAttribute(categoryId);
		return Result.ok(attributeList);
	}

	@Schema(description = "查询置顶频道页的三级分类")
	@GetMapping("/findTopBaseCategory3/{category1Id}")
	public Result<List<BaseCategory3>> findTopBaseCategory3(@PathVariable("category1Id") Long category1Id) {
		List<BaseCategory3> categoryList = baseCategoryService.findTopBaseCategory3(category1Id);
		return Result.ok(categoryList);
	}

	@Schema(description = "查询一级分类下的一二三级分类信息")
	@GetMapping("/getBaseCategoryList/{category1Id}")
	public Result<BaseCategoryVo> getBaseCategoryList(@PathVariable("category1Id") Long category1Id) {
		List<BaseCategoryVo> categoryList = baseCategoryService.getBaseCategoryList(category1Id);
		return Result.ok(categoryList.get(0));
	}

	/**
	 * 查询分类信息（远程调用）
	 * @param category3Id
	 * @return
	 */
	@GetMapping("getBaseCategoryByC3Id/{category3Id}")
	public Result<BaseCategoryView> getBaseCategoryByC3Id(@PathVariable("category3Id") Long category3Id) {
		BaseCategoryView categoryView = Db.lambdaQuery(BaseCategoryView.class)
				.eq(BaseCategoryView::getCategory3Id, category3Id).one();
		return Result.ok(categoryView);
	}
}

